package scau.redi.social_platform.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import scau.redi.social_platform.common.staticCode.Const;
import scau.redi.social_platform.common.staticCode.ResponseCode;
import scau.redi.social_platform.common.util.ResponseUtil;
import scau.redi.social_platform.domain.pojo.UserInfo;
import scau.redi.social_platform.service.AdminService;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * @Author: Redi
 * @Date: 2022/5/10 10:12
 * @Description:
 */
@RestController
@Api(value = "admin",description = "管理员操作")
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @GetMapping("/banUser.do")
    @ApiOperation("禁言用户（管理员）")
    public Map<String, Object> banUser(@RequestParam(value = "id", required = false)Integer id,
                                            HttpSession session
    ) {
        // 检查权限
        UserInfo user = (UserInfo) session.getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ResponseUtil.fail(ResponseCode.NEED_LOGIN.getDesc(), ResponseCode.NEED_LOGIN.getCode());
        }
        if (Const.Role.ROLE_ADMIN != user.getRoleId()) {
            return ResponseUtil.fail(ResponseCode.ILLEGAL_PERMIT.getDesc(), ResponseCode.ILLEGAL_PERMIT.getCode());
        }
        // 检查user status字段
        if (user.getStatusId() == Const.UserStatus.STATUS_BAN) {
            return ResponseUtil.fail(ResponseCode.BAN.getDesc(), ResponseCode.BAN.getCode());
        }
        // 创建更改的user对象
        UserInfo updatedUser = new UserInfo();
        updatedUser.setId(id);
        updatedUser.setStatusId(Const.UserStatus.STATUS_BAN);
        updatedUser.setUpdateTime(new Date());

        int banResultCode = adminService.banUser(updatedUser);
        if (banResultCode > 0) {
            return ResponseUtil.success("用户"+id+"已成功禁言");
        }
        return ResponseUtil.fail("用户"+id+"禁言操作失败");
    }

    @GetMapping("/unbanUser.do")
    @ApiOperation("解除禁言用户（管理员）")
    public Map<String, Object> unbanUser(@RequestParam(value = "id", required = false)Integer id,
                                            HttpSession session
    ) {
        // 检查权限
        UserInfo user = (UserInfo) session.getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ResponseUtil.fail(ResponseCode.NEED_LOGIN.getDesc(), ResponseCode.NEED_LOGIN.getCode());
        }
        if (Const.Role.ROLE_ADMIN != user.getRoleId()) {
            return ResponseUtil.fail(ResponseCode.ILLEGAL_PERMIT.getDesc(), ResponseCode.ILLEGAL_PERMIT.getCode());
        }
        // 检查user status字段
        if (user.getStatusId() == Const.UserStatus.STATUS_BAN) {
            return ResponseUtil.fail(ResponseCode.BAN.getDesc(), ResponseCode.BAN.getCode());
        }
        // 创建更改的user对象
        UserInfo updatedUser = new UserInfo();
        updatedUser.setId(id);
        updatedUser.setStatusId(Const.UserStatus.STATUS_NORMAL);
        updatedUser.setUpdateTime(new Date());

        int unbanResultCode = adminService.unbanUser(updatedUser);
        if (unbanResultCode > 0) {
            return ResponseUtil.success("用户"+id+"已成功解除禁言");
        }
        return ResponseUtil.fail("用户"+id+"解除禁言操作失败");
    }

    @GetMapping("/getAllUser.do")
    @ApiOperation("获取所有用户（管理员）")
    public Map<String, Object> banUser(@RequestParam(value = "page",defaultValue = "1")int page,
                                       @RequestParam(value = "size",defaultValue = "5")int size,
                                       HttpSession session){
        // 检查权限
        UserInfo user = (UserInfo) session.getAttribute(Const.CURRENT_USER);
        if (null == user) {
            return ResponseUtil.fail(ResponseCode.NEED_LOGIN.getDesc(), ResponseCode.NEED_LOGIN.getCode());
        }
        if (Const.Role.ROLE_ADMIN != user.getRoleId()) {
            return ResponseUtil.fail(ResponseCode.ILLEGAL_PERMIT.getDesc(), ResponseCode.ILLEGAL_PERMIT.getCode());
        }
        // 获取逻辑
        return ResponseUtil.success(adminService.findAllUser(page, size));
    }

}
